package com.gu.qqdemo.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gu.qqdemo.Bean.FriendBean;
import com.gu.qqdemo.R;
import com.gu.qqdemo.utils.pic.GlideApp;

import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.BaseViewHolder> {
    private List<FriendBean> list_adapter;
    private FriendBean friendBean;
    private Context context;

    public MessageAdapter(List<FriendBean> list_adapter) {
        this.list_adapter = list_adapter;
    }

    public List<FriendBean> getList_adapter() {
        return list_adapter;
    }

    public void setList_adapter(List<FriendBean> list_adapter) {
        this.list_adapter = list_adapter;
    }


    /**
     * 定义viewtype
     */
    private final int lun_img = 1;
    private final int recyclerview = 2;

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return lun_img;
        } else {
            return recyclerview;
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BaseViewHolder vh = null;
        switch (viewType) {
            case lun_img:
                View lview = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.lunbo_img, parent, false);
                vh = new LunboViewHolder(lview);
                break;
            case recyclerview:
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.message_adapter, parent, false);
                vh = new bodyview(view);
                break;
        }
        context = parent.getContext();
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {

        if (holder instanceof bodyview) {
            bodyview vh = (bodyview) holder;
            if (position < 1 + list_adapter.size()) {
                FriendBean bean = list_adapter.get(position - 1);
                vh.setDate(bean);
            }
        } else if (holder instanceof LunboViewHolder) {
            LunboViewHolder vh = (LunboViewHolder) holder;

        }


    }

    @Override
    public int getItemCount() {
        return list_adapter.size() + 1;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {


        public BaseViewHolder(View itemView) {
            super(itemView);

        }


    }

    public class LunboViewHolder extends BaseViewHolder {
        private ViewPager lunbo_vp;
        private int[] imgs = new int[]{R.mipmap.one, R.mipmap.two, R.mipmap.three, R.mipmap.foru};
        private Handler handler = new Handler();

        public LunboViewHolder(View itemView) {
            super(itemView);
            lunbo_vp = itemView.findViewById(R.id.lunbo_vp);


            LunboViewPager lunbo = new LunboViewPager(imgs);
            lunbo_vp.setAdapter(lunbo);
            int middle = lunbo.getCount() / 2;
            int start = middle - middle % 4;
            lunbo_vp.setCurrentItem(lunbo.getCount() / 2, false);
            handler.postDelayed(r, 2000);
            lunbo_vp.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            handler.removeCallbacks(r);
                            break;
                        case MotionEvent.ACTION_UP:

                        case MotionEvent.ACTION_CANCEL:
                            handler.postDelayed(r, 2000);
                            break;
                    }
                    return false;
                }
            });
        }

        private Runnable r = new Runnable() {
            @Override
            public void run() {
                lunbo_vp.setCurrentItem(lunbo_vp.getCurrentItem() + 1);
                handler.postDelayed(this, 2000);
            }
        };


    }

    public class bodyview extends BaseViewHolder {
        private ImageView img_adapter;
        private TextView tv_adapter;

        public bodyview(View itemView) {
            super(itemView);
            img_adapter = itemView.findViewById(R.id.img_adapter);
            tv_adapter = itemView.findViewById(R.id.tv_adapter);
        }

        public void setDate(FriendBean date) {
            GlideApp.with(context).load(date.getHeadUrl()).into(img_adapter);
            tv_adapter.setText(date.getName());
        }
    }

}
